package com.wzj.yygh.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzj.yygh.cmn.client.DictFeignClient;
import com.wzj.yygh.enums.DictEnum;
import com.wzj.yygh.model.user.Patient;
import com.wzj.yygh.user.mapper.PatientMapper;
import com.wzj.yygh.user.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements PatientService {
    @Autowired
    private DictFeignClient dictFeignClient;
    @Override
    public Patient getPatientById(Long id, Long userId) {
        LambdaQueryWrapper<Patient> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Patient::getUserId,userId).eq(Patient::getId,id);
        Patient patient = baseMapper.selectOne(queryWrapper);
        return this.packPatient(patient);
    }

    @Override
    public List<Patient> findAllByUserId(Long userId) {
        LambdaQueryWrapper<Patient> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Patient::getUserId,userId);
        List<Patient> patientList = baseMapper.selectList(queryWrapper);
        //封装数据
        patientList.forEach(this::packPatient);
        return patientList;
    }

    @Override
    public void removeByIdAndUserId(Long id, Long userId) {
        LambdaQueryWrapper<Patient> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Patient::getUserId,userId).eq(Patient::getId,id);
        baseMapper.delete(queryWrapper);
    }

    private Patient packPatient(Patient patient) {
        //根据证件类型编码，获取证件类型名称

        String certificatesTypeString = dictFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getCertificatesType());
        //区域
        String provinceString = dictFeignClient.getName(null, patient.getProvinceCode());
        String cityString = dictFeignClient.getName(null, patient.getCityCode());
        String districtString = dictFeignClient.getName(null,patient.getDistrictCode());

        patient.getParam().put("certificatesTypeString",certificatesTypeString);
        patient.getParam().put("provinceString",provinceString);
        patient.getParam().put("cityString",cityString);
        patient.getParam().put("districtString",districtString);
        patient.getParam().put("fullAddress",provinceString+cityString+districtString+patient.getAddress());

        return patient;
    }
}
